package com.firefly.admin.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.firefly.admin.common.annotations.annotation.OperationLog;
import com.firefly.admin.common.enums.OperationModuleEnum;
import com.firefly.admin.common.enums.OperationTypeEnum;
import com.firefly.admin.common.vo.Result;
import com.firefly.admin.system.entity.FySystemNotice;
import com.firefly.admin.system.entity.FySystemUser;
import com.firefly.admin.system.entity.FySystemUserNotice;
import com.firefly.admin.system.service.FySystemNoticeService;
import com.firefly.admin.system.vo.SelectOption;
import com.firefly.admin.system.vo.SystemNoticeQueryVo;
import com.firefly.admin.system.vo.SystemNoticeStatusUpdateVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "系统菜单管理接口API")
@RestController
@RequestMapping("/system/notice")
public class FySystemNoticeController {

    @Autowired
    private FySystemNoticeService fySystemNoticeService;

    /**
     * 分页查询系统通知接口
     *
     * @param systemNoticeQueryVo 系统通知查询条件
     * @return 系统通知信息列表
     */
    @ApiOperation(value = "分页查询系统通知接口", notes = "分页查询系统通知接口", httpMethod = "GET")
    @PreAuthorize(value = "hasAuthority('system:notice')")
    @GetMapping("/page")
    public Result<Page<FySystemNotice>> getSystemNoticeList(SystemNoticeQueryVo systemNoticeQueryVo) {
        return fySystemNoticeService.getSystemNoticeList(systemNoticeQueryVo);
    }

    /**
     * 查询已发布最新的系统公告接口
     *
     * @return 系统通知信息
     */
    @ApiOperation(value = "查询已发布最新的系统公告接口", notes = "查询已发布最新的系统公告接口", httpMethod = "GET")
    @GetMapping("/last")
    public Result<FySystemNotice> getLastSystemNotice() {
        return fySystemNoticeService.getLastSystemNotice();
    }

    /**
     * 查询已发布最新5条公告接口
     *
     * @return 系统通知信息列表
     */
    @ApiOperation(value = "查询已发布最新5条公告接口", notes = "查询已发布最新5条公告接口", httpMethod = "GET")
    @GetMapping("/top5/list")
    public Result<Map<String, List<FySystemNotice>>> getNoticeTop5List() {
        return fySystemNoticeService.getNoticeTop5List();
    }


    /**
     * 查询公告条数接口
     *
     * @return 条数
     */
    @ApiOperation(value = "查询公告条数接口", notes = "查询公告条数接口", httpMethod = "GET")
    @GetMapping("/unread/count")
    public Result<Integer> getUnReadNoticeCount() {
        return fySystemNoticeService.getUnReadNoticeCount();
    }

    /**
     * 获取系统通知信息接口
     *
     * @param noticeId 系统通知ID
     * @return 系统通知信息
     */
    @ApiOperation(value = "获取系统通知信息接口", notes = "通过通知ID获取通知详细信息", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", required = true, value = "系统通知ID", paramType = "path", dataType = "Long")
    })
    @GetMapping("/{noticeId}")
    public Result<FySystemNotice> getSystemNotice(@PathVariable Long noticeId) {
        return fySystemNoticeService.getSystemNotice(noticeId);
    }

    /**
     * 系统通知信息新增接口
     *
     * @param fySystemNotice 系统通知信息
     * @return 新增结果
     */
    @ApiOperation(value = "系统通知信息新增接口", notes = "系统通知信息新增接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_NOTICE, operateDesc = "系统通知新增")
    @PreAuthorize(value = "hasAuthority('system:notice:add')")
    @PostMapping("/add")
    public Result<FySystemNotice> add(@RequestBody FySystemNotice fySystemNotice) {
        return fySystemNoticeService.add(fySystemNotice);
    }

    /**
     * 系统通知信息编辑接口
     *
     * @param fySystemNotice 系统通知信息
     * @return 编辑结果
     */
    @ApiOperation(value = "系统通知信息编辑接口", notes = "系统通知信息编辑接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_NOTICE, operateDesc = "系统通知编辑")
    @PreAuthorize(value = "hasAuthority('system:notice:update')")
    @PostMapping("/update")
    public Result<FySystemNotice> update(@RequestBody FySystemNotice fySystemNotice) {
        return fySystemNoticeService.update(fySystemNotice);
    }

    /**
     * 系统通知状态修改接口
     *
     * @param systemNoticeStatusUpdateVo 系统通知状态编辑参数
     * @return 操作结果
     */
    @ApiOperation(value = "系统通知状态修改接口", notes = "系统通知状态修改接口", httpMethod = "POST")
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_NOTICE, operateDesc = "系统通知修改状态")
    @PreAuthorize(value = "hasAuthority('system:notice:update:status')")
    @PostMapping("/update/status")
    public Result<FySystemNotice> updateStatus(@RequestBody SystemNoticeStatusUpdateVo systemNoticeStatusUpdateVo) {
        return fySystemNoticeService.updateStatus(systemNoticeStatusUpdateVo);
    }

    /**
     * 系统通知信息删除接口
     *
     * @param noticeId 通知ID
     * @return 删除结果
     */
    @ApiOperation(value = "系统通知信息删除接口", notes = "系统通知信息删除接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", required = true, value = "系统通知ID", paramType = "query", dataType = "Long")
    })
    @OperationLog(module = OperationModuleEnum.SYSTEM_MANAGE, operateType = OperationTypeEnum.SYSTEM_NOTICE, operateDesc = "系统通知删除")
    @PreAuthorize(value = "hasAuthority('system:notice:delete')")
    @PostMapping("/delete")
    public Result<String> delete(Long noticeId) {
        return fySystemNoticeService.delete(noticeId);
    }

    /**
     * 系统用户通知已读接口
     *
     * @param noticeId 消息ID
     * @return 操作结果
     */
    @ApiOperation(value = "系统用户消息已读接口", notes = "系统用户消息已读接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", required = true, value = "系统通知ID", paramType = "query", dataType = "Long")
    })
    @PostMapping("/already/read")
    public Result<FySystemUserNotice> alreadyRead(Long noticeId) {
        return fySystemNoticeService.alreadyRead(noticeId);
    }

    /**
     * 查询用户通知公告列表接口
     *
     * @param noticeType 通知公告类型
     * @return 系统通知信息列表
     */
    @ApiOperation(value = "查询用户通知公告列表接口", notes = "查询用户通知公告列表接口", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeType", required = true, value = "通知公告类型", paramType = "path", dataType = "String")
    })
    @GetMapping("/list")
    public Result<List<FySystemNotice>> getNoticeList(String noticeType) {
        return fySystemNoticeService.getNoticeList(noticeType);
    }

    /**
     * 查询公告可通知对象列表
     *
     * @param noticeObjectType 公告通知对象类型
     * @return 可通知对象列表
     */
    @ApiOperation(value = "查询公告可通知对象列表", notes = "查询公告可通知对象列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeObjectType", required = true, value = "公告通知对象类型", paramType = "query", dataType = "String")
    })
    @GetMapping("/object/select/list")
    public Result<List<SelectOption>> getNoticeObjectSelectList(String noticeObjectType) {
        return fySystemNoticeService.getNoticeObjectSelectList(noticeObjectType);
    }

    /**
     * 根据公告ID查询公告通知对象列表
     *
     * @param noticeId 公告ID
     * @return 可通知对象列表
     */
    @ApiOperation(value = "根据公告ID查询公告通知对象列表", notes = "根据公告ID查询公告通知对象列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "noticeId", required = true, value = "公告ID", paramType = "query", dataType = "Long")
    })
    @GetMapping("/object/list")
    public Result<List<FySystemUser>> getNoticeObjectList(Long noticeId) {
        return fySystemNoticeService.getNoticeObjectList(noticeId);
    }
}
